package com.czl.lunt.controller;

import com.czl.lunt.common.Result;
import com.czl.lunt.common.ResultCode;
import com.czl.lunt.config.AppConfig;
import com.czl.lunt.model.Article;
import com.czl.lunt.model.Board;
import com.czl.lunt.model.User;
import com.czl.lunt.service.ArticleService;
import com.czl.lunt.service.BoardService;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Slf4j
@RestController
@RequestMapping("/article")
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    @Autowired
    private BoardService boardService;

    @Operation(description = "发布帖子")
    @PostMapping("/create")
    private Result create(HttpServletRequest request,
                          @RequestParam("boardId") @NonNull Long boardId,
                          @RequestParam("title") @NonNull String title,
                          @RequestParam("content") @NonNull String content){
        //判断用户是否被封禁
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        if(user.getState() != 0){//表示用户禁言
            return Result.failed(ResultCode.FAILED_USER_BANNED);
        }
        //检查当前板块是否存在问题
        Board board = boardService.selectById(boardId);
        if(board == null || board.getState() != 0 || board.getDeleteState() != 0){
            log.info("新增时板块禁用 或 板块异常");
            return Result.failed(ResultCode.FAILED_BOARD_NOT_EXISTS);
        }
        //封装文章对象
        Article article = new Article();
        article.setTitle(title);
        article.setContent(content);
        article.setBoardId(boardId);
        article.setUserId(user.getId());

        articleService.create(article);
        return Result.success("发帖成功");
    }
    @Operation(description = "展示页内帖子帖子")
    @GetMapping("/getAllByBoardId")
    private Result<List<Article>> getAllByBoardId(
            @RequestParam(value = "boardId", required = false) Long boardId){
        List<Article> articles = null;
        if(boardId == null){
            articles = articleService.selectAll();
        }else{
            articles = articleService.selectAllByBoardId(boardId);
        }
        if(articles == null || articles.size() <= 0){
            articles = new ArrayList<>();
        }
        return Result.success(articles);
    }


    @Operation(description = "展示帖子详情")
    @GetMapping("/details")
    private Result getDetail(HttpServletRequest request,
            @RequestParam("id")@NonNull Long id){
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);

        Article article = articleService.selectDetailById(id);
        if(article == null){
            return Result.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }
        if(Objects.equals(user.getId(), article.getUserId())){
            article.setOwn(true);
        }
        return Result.success(article);
    }
    @Operation(description = "编辑帖子")
    @PostMapping("/modify")
    private Result modify(HttpServletRequest request,
                         @RequestParam("id")@NonNull Long id,
                         @RequestParam("title")@NonNull String title,
                         @RequestParam("content")@NonNull String content){
        //必须判读用户是帖子的作者
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        if(user.getState() != 0){
            log.info("用户被封禁: {}", user.getId());
            return Result.failed(ResultCode.FAILED_USER_BANNED);
        }
        Article article = articleService.selectById(id);
        if(article == null){
            return Result.failed(ResultCode.FAILED_USER_NOT_EXISTS);
        }
        if(!Objects.equals(user.getId(), article.getUserId())){
            log.info("不能修改不属于自己的帖子 用户id = {}", user.getId());
            return Result.failed(ResultCode.FAILED_CREATE);
        }
        articleService.modify(id, title, content);
        log.info("帖子修改成功{}", id);

        return Result.success();
    }

    @Operation(description = "点赞帖子")
    @PostMapping("/thumbsUp")
    private Result thumbsUp(HttpServletRequest request,
            @RequestParam("id")@NonNull Long id){
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        if(user.getState() != 0){
            log.info("用户被封禁: {}", user.getId());
            return Result.failed(ResultCode.FAILED_USER_BANNED);
        }
        articleService.thumbsUpById(id);
        return Result.success();
    }

    @Operation(description = "删除帖子")
    @PostMapping("/delete")
    private Result deleteById(HttpServletRequest request,
            @RequestParam("id")@NonNull Long id){
        //用户状态判断
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        if(user.getState() != 0){
            log.info("用户被封禁: {}", user.getId());
            return Result.failed(ResultCode.FAILED_USER_BANNED);
        }
        Article article = articleService.selectById(id);
        if(article == null){
            return Result.failed(ResultCode.FAILED_USER_NOT_EXISTS);
        }
        if(!Objects.equals(user.getId(), article.getUserId())){
            log.info("不能删除不属于自己的帖子 用户id = {}", user.getId());
            return Result.failed("删除失败");
        }
        articleService.deleteById(id);
        log.info("帖子删除成功");
        return Result.success();

    }
}
